﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.ADService;
using System.Web.Security;
using System.IO;
using Avatar.Common;
using AvatarHelper;

public partial class Company_ADStep2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["ADCategoryID"] != null)
            {
                using (var _adc = DBHelper.GetDataContext())
                {
                    var _id = new Guid(Session["ADCategoryID"].ToString());
                    var _adCategory = _adc.ADCategory.Where(c=>c.ID==_id).FirstOrDefault();
                    lbl_ADCategory.Text = _adCategory.CategoryName;
                    lbl_WidthHeight.Text = _adCategory.Height.ToString() + "*" + _adCategory.Width;

                    ddl_Period.DataSource = _adCategory.ADPrice.Join(_adc.Period, price => price.PeriodID, period => period.ID, (price, period) => new { Name = period.Name, ID = period.ID });
                    ddl_Period.DataTextField = "name";
                    ddl_Period.DataValueField = "ID";
                    ddl_Period.DataBind();

                    lbl_Price.Text = _adCategory.ADPrice.Where(p => p.PeriodID == Convert.ToInt32(ddl_Period.SelectedValue)).FirstOrDefault() == null ? "0.00" : _adCategory.ADPrice.Where(p => p.PeriodID == Convert.ToInt32(ddl_Period.SelectedValue)).FirstOrDefault().Price.ToString();
                    if (Membership.GetUser() != null)
                    {
                        lbl_Balance.Text = _adc.CompanyFinancialAccount.Where(a => a.Company == Membership.GetUser().UserName).FirstOrDefault() == null ? "0.00" : _adc.CompanyFinancialAccount.Where(a => a.Company == Membership.GetUser().UserName).FirstOrDefault().Balance.ToString();
                    }
                }
            }
            else
            {
                Helper.ShowMassage("操作超时，请重新选择广告位置","./Company_ADStep1.aspx");
            }

            panel_AD.Visible = true;
            panel_Ok.Visible = false;
        }
    }
    protected void btn_Next_Click(object sender, EventArgs e)
    {
        try
        {

            Guid _id = Guid.Empty;
            string _user = Membership.GetUser().UserName;

            #region 入口检测

            if (Session["AdCategoryID"] == null) throw new Exception("操作超时，请返回上一步重新选择广告投放位置。");
            if (!FileUpload1.HasFile) throw new Exception("*请选择要上传的文件");

            Boolean _fileOK = false;
            String _fileExtension = Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] _allowedExtensions = { ".gif", ".jpeg", ".jpg" };
            foreach (var _e in _allowedExtensions)
            {
                if (_fileExtension.ToLower() == _e.ToLower())
                {
                    _fileOK = true;
                }
            }
            if (_fileOK != true) throw new Exception("*文件类型错误：只能上传是扩展名为:gif、jpeg、jpg的文件.");

            #endregion

            Guid _adCategory = new Guid(Session["ADCategoryID"].ToString());

            #region 图片上传

            var _year = DateTime.Now.Year.ToString();
            var _month = DateTime.Today.Month.ToString();
            var _day = DateTime.Today.Day.ToString();
            var _hour = DateTime.Now.Hour.ToString();
            var _minute = DateTime.Now.Minute.ToString();
            var _second = DateTime.Now.Second.ToString();

            string _path = "~/ad/" + _year + "/" + _month + "/" + _day + "/" + _hour + "/";
            String _savePath = Server.MapPath("~/ad/") + _year + "\\" + _month + "\\" + _day + "\\" + _hour + "\\";
            Directory.CreateDirectory(_savePath);

            string _fileName = Guid.NewGuid().ToString() + _fileExtension;

            string _iconURL = _savePath + _fileName;


            FileUpload1.PostedFile.SaveAs(_iconURL);

            #endregion

            ADManager.ADPublish(
                txt_Title.Text,
                _adCategory,
                _user,
                _path + _fileName,
                Convert.ToInt32(ddl_Period.SelectedValue));

            panel_AD.Visible = false;
            panel_Ok.Visible = true;
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
         finally
        {
            txt_Title.Text = "";
            FileUpload1.Dispose();
            ddl_Period.Dispose();
            lbl_Price.Text = "0.00";
            lbl_Balance.Text = "--";
        }
    }
    protected void ddl_Period_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Session["ADCategoryID"] != null)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _id = new Guid(Session["ADCategoryID"].ToString());
                var _adCategory = _adc.ADCategory.Where(c => c.ID == _id).FirstOrDefault();
                lbl_Price.Text = _adCategory.ADPrice.Where(p => p.PeriodID == Convert.ToInt32(ddl_Period.SelectedValue)).FirstOrDefault() == null ? "0.00" : _adCategory.ADPrice.Where(p => p.PeriodID == Convert.ToInt32(ddl_Period.SelectedValue)).FirstOrDefault().Price.ToString();
            }
        }
    }
}
